package com.example.restatr.Atcivity.Main;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.restatr.Atcivity.Main.Fragment.UserFragment;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.tabs.TabLayout;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private LinearLayout checkbox_LL;
    private EditText userName;
    private EditText password;
    private EditText phone;
    private CheckBox RemPsw;
    private CheckBox AutoLogin;
    private MaterialButton button;
    private TabLayout cardTab;
    private TabLayout.Tab login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.activity_login_at);

        tb = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        checkbox_LL = findViewById(R.id.checkbox_LL);
        userName = findViewById(R.id.userName);
        password = findViewById(R.id.password);
        RemPsw = findViewById(R.id.RemPsw);
        AutoLogin = findViewById(R.id.AutoLogin);
        phone = findViewById(R.id.Phone);
        button = findViewById(R.id.button);
        cardTab = findViewById(R.id.card_tab);
        login = cardTab.getTabAt(0);
        boolean isRemPsw = MainActivity.sp.getBoolean("isRemPsw", true);
        boolean isAutoLogin = MainActivity.sp.getBoolean("isAutoLogin", true);
        RemPsw.setChecked(isRemPsw);
        RemPsw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RemPsw.isChecked()) {
                    MainActivity.edit.putBoolean("isRemPsw", true).apply();
                } else {
                    MainActivity.edit.putBoolean("isRemPsw", false);
                    MainActivity.edit.putString("username", "");
                    MainActivity.edit.putString("password", "");
                    MainActivity.edit.apply();
                }
            }
        });
        if (isRemPsw) {
            userName.setText(MainActivity.sp.getString("username", null));
            password.setText(MainActivity.sp.getString("password", null));
        } else {
            userName.setText("");
            password.setText("");
        }

        AutoLogin.setChecked(isAutoLogin);
        AutoLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AutoLogin.isChecked()) {
                    MainActivity.edit.putBoolean("isAutoLogin", true).apply();
                } else {
                    MainActivity.edit.putBoolean("isAutoLogin", false).apply();
                }
            }
        });
        Tools.SetBarTitle( tb,barTitle,"登入",search,LoginAt.this,false );
        cardTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getText().toString()) {
                    case "登入":
                        button.setText("登入");
                        phone.setVisibility(View.GONE);
                        checkbox_LL.setVisibility(View.VISIBLE);
                        break;
                    case "注册":
                        button.setText("注册");
                        checkbox_LL.setVisibility(View.GONE);
                        phone.setVisibility(View.VISIBLE);
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String UserName = userName.getText().toString();
                final String Password = password.getText().toString();
                final String Phone = phone.getText().toString();
                //登入
                if (cardTab.getSelectedTabPosition() == 0) {
                    barTitle.setText( "登入" );
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put("username", UserName);
                                jsonObject.put("password", Password);
                                final String post = Tools.Post("/prod-api/api/login", "", jsonObject.toString());
                                if (post != null) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            try {
                                                JSONObject jiexi = new JSONObject(post);
                                                Toast.makeText(LoginAt.this, jiexi.getString("msg"), Toast.LENGTH_SHORT).show();
                                                UserFragment.code = jiexi.getInt("code");
                                                if (UserFragment.code == 200) {
                                                    UserFragment.token = jiexi.getString("token");
                                                    if (RemPsw.isChecked()) {
                                                        MainActivity.edit.putString("username", UserName);
                                                        MainActivity.edit.putString("password", Password);
                                                        MainActivity.edit.putBoolean("isRemPsw", true);
                                                        MainActivity.edit.apply();
                                                    }
                                                    if (AutoLogin.isChecked()) {
                                                        MainActivity.edit.putInt("code", jiexi.getInt("code"));
                                                        MainActivity.edit.putString("token", jiexi.getString("token"));
                                                        MainActivity.edit.putBoolean("isAutoLogin", true);
                                                        MainActivity.edit.apply();
                                                    }
                                                    finish();
                                                }
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    });
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    }).start();
                } else {
                    barTitle.setText( "注册" );
                    //注册
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put("userName", UserName);
                                jsonObject.put("nickName", "新用户");
                                jsonObject.put("password", Password);
                                jsonObject.put("phonenumber", Phone);
                                final String post = Tools.Post("/prod-api/api/register", "", jsonObject.toString());
                                if (post != null) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            try {
                                                JSONObject jiexi = new JSONObject(post);
                                                Toast.makeText(LoginAt.this, jiexi.getString("msg"), Toast.LENGTH_SHORT).show();
                                                if (jiexi.getInt("code") == 200) {
                                                    login.select();
                                                }
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    });

                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }
            }
        });

    }
}